#include "MessageDialog.h"

MessageWidget::MessageWidget(inboxListData *data, inboxListData *trushData, contactData *cdata, int index):QWidget()
{
	mData = data;
	mTrushData = trushData;
	cData = cdata;
	mIndex = index;
	mMessageList = &(mData->mInboxList[mIndex].mMessageList);
	QString phoneNum = data->mInboxList[index].phoneNum;
	contactName = cdata->getNameForNum(phoneNum);
	startY = 0;
	theFirstTime = true;

	QCursor cursor;
	cursor.setShape(Qt::PointingHandCursor);
	this->setCursor(cursor);

	repaint();
}

MessageWidget::~MessageWidget()
{
	qDebug()<<"MessageWidget destructor";
}

#if 1
void MessageWidget::paintEvent(QPaintEvent *e)
{
	QPixmap pix(this->width(), this->height()-3);
	pix.fill(QColor(255,255,255));

	//QPixmap textPix;
	//QPainter p(&textPix);
	QPainter p(this);
	p.drawPixmap(0, 0, pix);
	p.setPen(QColor(255,0,0));

	int height=startY;
	textHeight = 0;
	smsYList.clear();
	for(int i=0; i<mMessageList->count(); i++)
	{
		smsYList.push_back(textHeight);
		QString str = mMessageList->at(i).content;
		QString type = mMessageList->at(i).type;
		QTextOption textOption;
		textOption.setWrapMode(QTextOption::WordWrap);
		QRectF rect;
		QString drawStr;
		if(type == QString("from"))
		{
			drawStr = cntr("%1:%2").arg(contactName).arg(str);
			rect = p.boundingRect(QRectF(0,height,210,0), drawStr, textOption);
			p.setPen(QColor(255,255,255));
			p.setBrush(QColor(0,255,0));
			p.drawRect(rect);
			p.setPen(QColor(0,0,0));
		}else{
			drawStr = cntr("我:%1").arg(str);
			rect = p.boundingRect(QRectF(130,height,210,0), drawStr, textOption);
			QRectF rectmp = rect;
			rect = QRectF(320-rectmp.width(), rectmp.y(), rectmp.width(), rectmp.height());//放到最右边
			p.setPen(QColor(255,255,0));
			p.setBrush(QColor(255,255,0));
			p.drawRect(rect);
			p.setPen(QColor(0,0,0));
		}
		height +=(rect.height() + 5);
		textHeight += (rect.height() + 5);
		p.drawText(rect, drawStr, textOption);
	}
	smsYList.push_back(textHeight);
	if(theFirstTime == true)
	{
		printf("firsttime height = %d, textHeight = %d\n", this->height(), textHeight);
		startY = this->height() - textHeight;
		if(startY > 0) startY = 0;
		theFirstTime = false;
	}
}
#endif


void MessageWidget::mousePressEvent(QMouseEvent *e)
{
	mousePoint = e->pos();
	mousePressed = true;
	mouseMoved = false;
}

void MessageWidget::mouseReleaseEvent(QMouseEvent *e)
{
	if(mouseMoved == false)
	{
		printf("mouse clicked\n");
		int tmpY = e->y();
		for(int i=0; i<smsYList.count() - 1; i++)
		{
			if(smsYList[i] <= tmpY - startY && smsYList[i+1] >= tmpY - startY)
			{
				printf("index = %d\n", i);
				//TODO 弹出对话框(删除，移动，取消)
				controlMessage(i);
				break;
			}
		}
	}
	mousePressed = false;
	int x = e->x();
	int y = e->y();
	printf("x = %d, y= %d\n", x, y);
	QCursor cursor;
	cursor.setShape(Qt::ArrowCursor);
	repaint();
}

void MessageWidget::mouseMoveEvent(QMouseEvent *e)
{
	if(mousePressed)
	{
		int nowY = e->y();
		int delta = nowY-mousePoint.y();
		if(delta != 0) 
		{
			mouseMoved = true;
		}
		if(startY + delta <=0 && startY +delta >= (height() - textHeight))
			startY +=delta;
		this->repaint();
		mousePoint = e->pos();
	}
}


void MessageWidget::wheelEvent(QWheelEvent *e)
{
	int delta = e->delta();
	if(textHeight <= this->height()) return;
	if(delta<0)
	{
		if(textHeight - this->height() + startY - WHEEL_STEP >0 ) startY-=WHEEL_STEP;
		else{
				startY = this->height() - textHeight;
			}
	}else{
		if(startY + WHEEL_STEP <= 0) startY+=WHEEL_STEP;
		else startY = 0;
	}
	repaint();
}

void MessageWidget::controlMessage(int index)
{
	int result;
	controlMessageDialog *mMessageDialog = new controlMessageDialog();
	mMessageDialog->contentLabel->setText(mMessageList->at(index).content);
	mMessageDialog->exec();
	result = mMessageDialog->result;
	delete mMessageDialog;
	printf("result = %d\n", result);
	if(result == 0)//删除短信
	{
		bool tmpAns = mData->delMessage(mData->mInboxList[mIndex].phoneNum,index);
		if(tmpAns)
		{
			printf("in Null -------------------\n");
			emit quitWidget();
		}
		repaint();
	}else if(result == 1 )//移动消息到另一个数据库中
	{
		//1.查找有无当前联系人
		QString tmpstr = mData->mInboxList[mIndex].phoneNum;
		smsData tmpSmsData = mData->mInboxList[mIndex].mMessageList[index];
		mTrushData->insertMessage(tmpstr, tmpSmsData);
		bool tmpAns = mData->delMessage(mData->mInboxList[mIndex].phoneNum,index);
		if(tmpAns)
		{
			printf("in Null -------------------\n");
			emit quitWidget();
		}
		repaint();
	}
}


MessageDialog::MessageDialog(inboxListData *data, inboxListData *trushData, contactData *cdata, int index)
{
	myDialogUi.setupUi(this);

	QString phoneNum = data->mInboxList[index].phoneNum;
	QString name = cdata->getNameForNum(phoneNum);
	QString title = cntr("与[%1]的会话").arg(name);
	setWindowTitle(title);

	contentWidget = new MessageWidget(data, trushData, cdata, index);
	myDialogUi.verticalLayout->addWidget(contentWidget);
	qDebug()<<"width.h = "<<contentWidget->height();
	connect(contentWidget, SIGNAL(quitWidget()), this, SLOT(reject()));
}
MessageDialog::~MessageDialog()
{
	qDebug()<<"MessageDialog destructor";
}

